package com.beiding.controller;

import com.beiding.exception.NotFoundException;
import com.beiding.service.OrderService;
import com.beiding.pojo.order.Evaluation;
import com.beiding.pojo.order.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController {

    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }


    //发起订单
    @PostMapping("/reserve")
    @Secured("ROLE_TENANT")
    String reserve(Principal principal, String roomId, int serial, int count) {
        Order order = new Order();
        order.setTenant(principal.getName());
        order.setRoomId(roomId);
        order.setPriceSerial(serial);
        order.setPriceCount(count);
        order.setState(Order.CREATE);
        order.setCreateTimestamp(System.currentTimeMillis());

        //保存订单,返回订单id
        return orderService.save(order);
    }


    //获取入住码
    @GetMapping("/checkInKey")
    @Secured("ROLE_TENANT")
    String checkInKey(Principal principal, String id) {
        orderService.checkHolder(id, principal.getName());
        Order order = orderService.get(id, "checkInKey");

        String checkInKey = order.getCheckInKey();

        if (checkInKey == null) {
            throw new NotFoundException("当前订单尚未受理,没有入住码");
        }

        return checkInKey;
    }


    //获取未付款订单
    @GetMapping("/non-payment-order")
    @Secured("ROLE_TENANT")
    List<Order> nonPaymentOrders(Principal principal) {
        return orderService.getNonPayment(principal.getName());
    }

    //获取已付款订单
    @GetMapping("/paid-order")
    @Secured("ROLE_TENANT")
    List<Order> paidOrders(Principal principal) {
        return orderService.getPayed(principal.getName());
    }

    //获取待评价订单
    @GetMapping("/evaluate-order")
    @Secured("ROLE_TENANT")
    List<Order> evaluateOrders(Principal principal) {
        return orderService.getEvaluate(principal.getName());
    }


    //评价一个订单
    @PostMapping("/evaluate")
    @Secured("ROLE_TENANT")
    void evaluate(Principal principal, String orderId, Evaluation evaluation) {
        //检查订单持有
        orderService.checkHolder(orderId, principal.getName());

        Assert.isTrue(evaluation.getEvaluation().length() > 2 && evaluation.getEvaluation().length() < 150, "评价字数在2到150字之间");

        Assert.isTrue(evaluation.getEnvironment() > 0 && evaluation.getEnvironment() < 6, "环境评分介于1到5");
        Assert.isTrue(evaluation.getService() > 0 && evaluation.getService() < 6, "服务评分介于1到5");

        //评价
        orderService.evaluate(orderId, evaluation);

    }


    //获取一个订单.只返回必要的信息
    @GetMapping("/get")
    @Secured("ROLE_TENANT")
    Order get(Principal principal, String id) {
        orderService.checkHolder(id, principal.getName());

        return orderService.get(id, "priceCount", "priceSerial", "id", "state", "room", "refundReason", "price", "cost");
    }

    //移除一个订单
    @GetMapping("/remove")
    @Secured("ROLE_TENANT")
    String remove(Principal principal, String id) {

        //检查订单的归属
        orderService.checkHolder(id, principal.getName());

        //移除订单
        orderService.remove(id);

        //返回订单id
        return id;
    }


    //是否同意扣留押金
    @PostMapping("/transact/withholdDeposit")
    @Secured("ROLE_TENANT")
    void withholdDeposit(Principal principal, String id, boolean agree) {

        orderService.checkHolder(id, principal.getName());

        if (agree) {
            orderService.agreeWithholdDeposit(id);
        }else{
            orderService.disagreeWithholdDeposit(id);}
    }


    @PostMapping("/refund/revocation")
    @Secured("ROLE_TENANT")
    int revocation(Principal principal, String id) {

        orderService.checkHolder(id, principal.getName());

        //返回发起退款的次数
        return orderService.revocationRefund(id);
    }


    @PostMapping("/transact/refund/arbitration")
    @Secured("ROLE_TENANT")
    void arbitration(Principal principal, String id) {
        orderService.checkHolder(id, principal.getName());

        //退款仲裁
        orderService.arbitrationRefund(id);

    }


}
